package com.aabte.cases;

import com.aabte.config.TestConfig;
import com.aabte.model.InterfaceName;
import com.aabte.model.LoginCase;
import com.aabte.utils.ConfigFile;
import com.aabte.utils.DatabaseUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.session.SqlSession;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

public class LoginTest {

    @BeforeTest(groups = "loginTrue", description = "测试准备工作，获取HttpClient等")
    public void beforeTest(){
        TestConfig.getUserInfoUrl = ConfigFile.getUrl(InterfaceName.GET_USER_LIST);
        TestConfig.getUserListUrl = ConfigFile.getUrl(InterfaceName.GET_USER_LIST);
        TestConfig.addUserUrl = ConfigFile.getUrl(InterfaceName.ADD_USER);
        TestConfig.updateUserInfoUrl = ConfigFile.getUrl(InterfaceName.UPDATE_USER_INFO);
        TestConfig.loginUrl = ConfigFile.getUrl(InterfaceName.LOGIN);

        TestConfig.cookieStore = new BasicCookieStore();

        TestConfig.defaultHttpClient = new DefaultHttpClient();
        TestConfig.httpClient = HttpClients
                .custom()
                .setDefaultCookieStore(TestConfig.cookieStore)
                .build();

    }

    @Test(groups = "loginTrue", description = "用户登陆成功接口测试")
    public void loginTrue() throws IOException {
        SqlSession session = DatabaseUtil.getSqlSession();
        LoginCase loginCase = session.selectOne("loginCase", 1);
        System.out.println(loginCase);
        System.out.println(TestConfig.loginUrl);

        Boolean isLogin = getJsonResult(loginCase);

        boolean flag = verifyCookie();

        Assert.assertTrue(isLogin);
        Assert.assertTrue(flag);
    }

    @Test(groups = "loginFalse", description = "用户登陆失败接口测试")
    public void loginFalse() throws IOException {
        SqlSession session = DatabaseUtil.getSqlSession();
        LoginCase loginCase = session.selectOne("loginCase", 2);
        System.out.println(loginCase);
        System.out.println(TestConfig.loginUrl);

        Boolean isLogin = getJsonResult(loginCase);

        boolean flag = verifyCookie();

        Assert.assertFalse(isLogin);
        //Assert.assertFalse(flag);

    }

    private boolean verifyCookie() {
        boolean flag = false;
        List<Cookie> cookies = TestConfig.cookieStore.getCookies();
        if (!Objects.isNull(cookies)) {
            for (Cookie cookie :
                    cookies) {
                if (cookie.getName().equals("login") && "true".equals(cookie.getValue())) {
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    private Boolean getJsonResult(LoginCase loginCase) throws IOException {

        HttpPost post = new HttpPost(TestConfig.loginUrl);
        JSONObject param = new JSONObject();
        param.put("username", loginCase.getUsername());
        param.put("password", loginCase.getPassword());
        StringEntity entity = new StringEntity(param.toString(), "utf-8");
        post.setEntity(entity);

        post.setHeader("Content-type", "application/json");

        HttpResponse response = TestConfig.httpClient.execute(post);

        String result = EntityUtils.toString(response.getEntity());

        Boolean isLogin = Boolean.valueOf(result);
        return isLogin;

    }



}
